//
//  ZBShareMenuView.m
//  MessageDisplay
//
//  Created by zhoubin@moshi on 14-5-13.
//  Copyright (c) 2014年 Crius_ZB. All rights reserved.
//

// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com

#import "MessagePhotoView.h"
#import "ZYQAssetPickerController.h"
// 每行有4个
#define kZBMessageShareMenuPerRowItemCount 4
#define kZBMessageShareMenuPerColum 2

#define kZBShareMenuItemIconSize 60
#define KZBShareMenuItemHeight 80

#define MaxItemCount 10
#define ItemWidth 94
#define ItemHeight 94


@interface MessagePhotoView ()


/**
 *  这是背景滚动视图
 */
@property (nonatomic, strong) UIScrollView *photoScrollView;
@property (nonatomic, weak) UIScrollView *shareMenuScrollView;
@property (nonatomic, weak) UIPageControl *shareMenuPageControl;
@property (nonatomic, weak)UIButton *btnviewphoto;


@property (nonatomic, retain)UILabel *lblNum;
//@property (nonatomic, strong) NSMutableArray *photoMenuItems;
@property(nonatomic,strong) NSMutableArray *itemArray;
@end

@implementation MessagePhotoView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}


- (void)setup{
   
    self.backgroundColor = [UIColor whiteColor];

    _photoScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 10, 320, 124)];
   // _photoScrollView.backgroundColor = [UIColor redColor];
    _photoScrollView.contentSize = CGSizeMake(1024, 124);
    _photoMenuItems = [[NSMutableArray alloc]init];
    _itemArray = [[NSMutableArray alloc]init];
    [self addSubview:_photoScrollView];
    _lblNum = [[UILabel alloc]initWithFrame:CGRectMake(0, _photoScrollView.bottom + 10, 230, 30)];
   
    [self addSubview:_lblNum];
    
    [self initlizerScrollView:self.photoMenuItems];

}

-(void)reloadDataWithImage:(UIImage *)image{
    [self.photoMenuItems addObject:image];
    
    [self initlizerScrollView:self.photoMenuItems];
}
- (void)reloadData
{
    
}
-(void)initlizerScrollView:(NSArray *)imgList{
    [self.photoScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    for(int i = 0;i < imgList.count; i++){
        
        ALAsset *asset=imgList[i];
         UIImage *tempImg=[UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
        MessagePhotoMenuItem *photoItem = [[MessagePhotoMenuItem alloc]initWithFrame:CGRectMake(10+ i * (ItemWidth + 5 ), 20, ItemWidth, ItemHeight)];
        photoItem.delegate = self;
        photoItem.index = i;
        photoItem.contentImage = tempImg;
        [self.photoScrollView addSubview:photoItem];
        [self.itemArray addObject:photoItem];
    }
    if(imgList.count < MaxItemCount){
        UIButton *btnphoto=[UIButton buttonWithType:UIButtonTypeCustom];
        [btnphoto setFrame:CGRectMake(20 + (ItemWidth + 5) * imgList.count, 20, 84, 84)];
        [btnphoto setImage:[UIImage imageNamed:@"addImage.png"] forState:UIControlStateNormal];
        [btnphoto addTarget:self action:@selector(localPhoto) forControlEvents:UIControlEventTouchUpInside];
        [self.photoScrollView addSubview:btnphoto];
    }
    
    NSInteger count = MIN(imgList.count + 1, MaxItemCount);
     _lblNum.text = [NSString stringWithFormat:@"共选了%ld张，共可选10张",self.photoMenuItems.count];
    _lblNum.backgroundColor = [UIColor clearColor];
    [self.photoScrollView setContentSize:CGSizeMake(20 + (ItemWidth + 5)*count, 0)];
}


//打开相册，可以多选
-(void)localPhoto{
    ZYQAssetPickerController *picker = [[ZYQAssetPickerController alloc]init];
    
    picker.maximumNumberOfSelection = 10;
    picker.assetsFilter = [ALAssetsFilter allPhotos];
    picker.showEmptyGroups = NO;
    picker.delegate = self;
    picker.selectionFilter = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject,NSDictionary *bindings){
        if ([[(ALAsset *)evaluatedObject valueForProperty:ALAssetPropertyType]isEqual:ALAssetTypeVideo]) {
            NSTimeInterval duration = [[(ALAsset *)evaluatedObject valueForProperty:ALAssetPropertyDuration]doubleValue];
            return duration >= 5;
        }else{
            return  YES;
        }
    }];
    
    [self.delegate addPicker:picker];
}

/*
 得到选中的图片
 */
#pragma mark - ZYQAssetPickerController Delegate

-(void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)assets{
   
    [self.scrollview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

 
  NSArray *array = [self.photoMenuItems arrayByAddingObjectsFromArray:assets];
  self.photoMenuItems = [NSMutableArray arrayWithArray:array];
    [self initlizerScrollView:_photoMenuItems];
    [_delegate getPhotoMenuItems:self.photoMenuItems];
   [picker dismissViewControllerAnimated:YES completion:Nil];
}


- (void)dealloc {
    
    self.photoScrollView.delegate = nil;
    self.shareMenuScrollView.delegate = nil;
    self.shareMenuScrollView = nil;
    self.shareMenuPageControl = nil;
}

#pragma mark - MessagePhotoItemDelegate

-(void)messagePhotoItemView:(MessagePhotoMenuItem *)messagePhotoItemView didSelectDeleteButtonAtIndex:(NSInteger)index{
    [self.photoMenuItems removeObjectAtIndex:index];
    if (self.photoMenuItems.count == 0) {
        [self.delegate removeImageView];
    }
    [_delegate getPhotoMenuItems:self.photoMenuItems];
    [self initlizerScrollView:self.photoMenuItems];
}

#pragma mark - UIScrollView delegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //每页宽度
    CGFloat pageWidth = scrollView.frame.size.width;
    //根据当前的坐标与页宽计算当前页码
    NSInteger currentPage = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth)+1;
    [self.shareMenuPageControl setCurrentPage:currentPage];
}




@end
